/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelLogin.java
 *
 * Created on Dec 3, 2010, 10:39:16 PM
 */
package stta.elinfo.perpustakaan.gui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
import stta.elinfo.perpustakaan.dao.entity.Buku;
import stta.elinfo.perpustakaan.dao.entity.Kategori;
import stta.elinfo.perpustakaan.dao.impl.BukuImpl;
import stta.elinfo.perpustakaan.dao.impl.KategoriImpl;
import stta.elinfo.perpustakaan.dao.interf.BukuInterf;
import stta.elinfo.perpustakaan.dao.interf.KategoriInterf;

/**
 *
 * @author fendi
 */
public class PanelTambah extends javax.swing.JPanel {

    JFileChooser choser = new JFileChooser();
    String dir, tujuan;
    File file;
    KategoriInterf kategori;
    BukuInterf buku;

    /** Creates new form PanelLogin */
    public PanelTambah() {
        initComponents();
        setSize(600, 400);

        kategori = new KategoriImpl();
        buku = new BukuImpl();
        dir = System.getProperty("user.dir");
        FileNameExtensionFilter filter = new FileNameExtensionFilter("ebook file", "pdf", "doc", "docx", "ppt", "pptx");
        choser.addChoosableFileFilter(filter);
        // choser.setCurrentDirectory(new File(dir));
        loadCombo();
    }

    void loadCombo() {
        try {
            combokategori.removeAllItems();
            for (Kategori kat : kategori.getAll()) {
                combokategori.addItem(kat.getKategori());
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelTambah.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void kosongkanteks() {
        txtnama.setText("");
        txtfile.setText("");
        txtDiskripsi.setText("");
    }

    boolean cekteks() {
        if (!txtnama.getText().isEmpty()
                && !txtfile.getText().isEmpty()
                && !txtDiskripsi.getText().isEmpty()) {
            return true;
        } else {
            return false;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jProgressBar1 = new javax.swing.JProgressBar();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        txtnama = new stta.elinfo.perpustakaan.template.TextField();
        txttanggal = new stta.elinfo.perpustakaan.template.Date();
        txtDiskripsi = new stta.elinfo.perpustakaan.template.TextField();
        txtfile = new stta.elinfo.perpustakaan.template.TextField();
        combokategori = new javax.swing.JComboBox();
        button1 = new stta.elinfo.perpustakaan.template.Button();
        btnBrow = new stta.elinfo.perpustakaan.template.Button();
        button3 = new stta.elinfo.perpustakaan.template.Button();
        button4 = new stta.elinfo.perpustakaan.template.Button();
        jLabel7 = new javax.swing.JLabel();

        setForeground(new java.awt.Color(255, 255, 255));
        setOpaque(false);

        jLabel1.setFont(new java.awt.Font("Comic Sans MS", 3, 18));
        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setText("Tambah File");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setForeground(new java.awt.Color(255, 255, 255));
        jLabel2.setText("Nama Ebook");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel3.setForeground(new java.awt.Color(255, 255, 255));
        jLabel3.setText("Kategori");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel4.setForeground(new java.awt.Color(255, 255, 255));
        jLabel4.setText("Tanggal");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel5.setForeground(new java.awt.Color(255, 255, 255));
        jLabel5.setText("Descripsi");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel6.setForeground(new java.awt.Color(255, 255, 255));
        jLabel6.setText("File");

        txttanggal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd MMMM yyyy"))));
        txttanggal.setValue(new java.util.Date());
        txttanggal.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txttanggalActionPerformed(evt);
            }
        });

        txtfile.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtfileMouseClicked(evt);
            }
        });
        txtfile.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtfileKeyPressed(evt);
            }
        });

        combokategori.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        combokategori.setFocusTraversalPolicyProvider(true);

        button1.setText("new Kategori");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        btnBrow.setText("browse");
        btnBrow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowActionPerformed(evt);
            }
        });

        button3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/stta/elinfo/perpustakaan/gambar/arrow_refresh.png"))); // NOI18N
        button3.setText("Reset");

        button4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/stta/elinfo/perpustakaan/gambar/disk.png"))); // NOI18N
        button4.setText("Simpan");
        button4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button4ActionPerformed(evt);
            }
        });

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel7.setForeground(new java.awt.Color(255, 255, 255));
        jLabel7.setText("Boleh Diganti");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(combokategori, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtnama, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtfile, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnBrow, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE)
                            .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtDiskripsi, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(98, 98, 98)
                                .addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(txttanggal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 240, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(51, 51, 51)))
                .addGap(51, 51, 51))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 428, Short.MAX_VALUE)
                .addGap(123, 123, 123))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnBrow, button1, button3, button4});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(109, 109, 109)
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtfile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(btnBrow, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtnama, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(combokategori, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtDiskripsi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txttanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(button3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(button4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(61, 61, 61))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
        // TODO add your handling code here:
        String kat = JOptionPane.showInputDialog("masukkan kategori baru yg sesuai");
        if (kat == null) {
            return;
        } else {
            try {
                this.kategori.insert(kat);
                loadCombo();
            } catch (SQLException ex) {
                Logger.getLogger(PanelTambah.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_button1ActionPerformed

    private void btnBrowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowActionPerformed
        // TODO add your handling code here:
        int ren = choser.showOpenDialog(this);
        if (ren == JFileChooser.APPROVE_OPTION) {
            file = choser.getSelectedFile();
            txtfile.setText(file.getAbsolutePath());
            txtnama.setText(file.getName());
        }
    }//GEN-LAST:event_btnBrowActionPerformed

    private void txtfileKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtfileKeyPressed
        // TODO add your handling code here:
        int ren = choser.showOpenDialog(this);
        if (ren == JFileChooser.APPROVE_OPTION) {
            file = choser.getSelectedFile();
            txtfile.setText(file.getName());
        }
    }//GEN-LAST:event_txtfileKeyPressed

    private void button4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button4ActionPerformed
        // TODO add your handling code here:
        if (cekteks()) {
            InputStream in = null;
            String direktori = dir.substring(0, dir.length() - 5) + "/arsip/";
            System.out.println(direktori);
            File cek=new File(direktori);
            try {
                if (cek.mkdirs()) {
                
                  
                        System.out.println("multi direktori berhasil dibuat");
                    } else {
                        System.out.println("multi direktori gagal dibuat");
                    }
                
                System.out.println("jalan sampe sini");
                tujuan = dir.substring(0, dir.length() - 5) + "/arsip/" + file.getName();
                Buku bk = new Buku();
                bk.setNama(txtnama.getText());
                bk.setDeskripsi(txtDiskripsi.getText());
                bk.setKategori((String) combokategori.getSelectedItem());
                bk.setTanggal((Date) txttanggal.getValue());
                bk.setPath("/arsip/" + file.getName());
                try {
                    this.buku.insert(bk);
                } catch (SQLException ex) {
                    Logger.getLogger(PanelTambah.class.getName()).log(Level.SEVERE, null, ex);
                }
                JOptionPane.showMessageDialog(this, "buku sudah di simpan di aplikasi");
                this.kosongkanteks();
                File f1 = new File(file.getAbsolutePath());
                File f2 = new File(tujuan);
                in = new FileInputStream(f1);
                OutputStream out = new FileOutputStream(f2);
                byte[] buf = new byte[1024];
                int len;
                try {
                    while ((len = in.read(buf)) > 0) {
                        try {
                            out.write(buf, 0, len);
                        } catch (IOException ex) {
                            Logger.getLogger(PanelTambah.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                } catch (IOException ex) {
                    Logger.getLogger(PanelTambah.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(PanelTambah.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                try {
                    in.close();
                } catch (IOException ex) {
                    Logger.getLogger(PanelTambah.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            JOptionPane.showMessageDialog(this, "data harus diisi semua", "peringatan", JOptionPane.WARNING_MESSAGE);
            kosongkanteks();
        }
    }//GEN-LAST:event_button4ActionPerformed

    private void txtfileMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_txtfileMouseClicked
        // TODO add your handling code here:
        int ren = choser.showOpenDialog(this);
        if (ren == JFileChooser.APPROVE_OPTION) {
            file = choser.getSelectedFile();
            txtfile.setText(file.getAbsolutePath());
            txtnama.setText(file.getName());
        }
    }//GEN-LAST:event_txtfileMouseClicked

    private void txttanggalActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txttanggalActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txttanggalActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private stta.elinfo.perpustakaan.template.Button btnBrow;
    private stta.elinfo.perpustakaan.template.Button button1;
    private stta.elinfo.perpustakaan.template.Button button3;
    private stta.elinfo.perpustakaan.template.Button button4;
    private javax.swing.JComboBox combokategori;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JProgressBar jProgressBar1;
    private stta.elinfo.perpustakaan.template.TextField txtDiskripsi;
    private stta.elinfo.perpustakaan.template.TextField txtfile;
    private stta.elinfo.perpustakaan.template.TextField txtnama;
    private stta.elinfo.perpustakaan.template.Date txttanggal;
    // End of variables declaration//GEN-END:variables
}
